package com.wlxk.crawler.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlxk.crawler.core.dao.ArticleDAO;
import com.wlxk.crawler.core.dao.ChapterDAO;
import com.wlxk.crawler.core.entity.ArticleDO;
import com.wlxk.crawler.core.entity.ChapterDO;
import com.wlxk.crawler.core.exception.WlxkException;
import com.wlxk.crawler.core.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description:
 * @author: 21054523
 * @time: 2023/10/26 10:45
 */
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleDAO articleDAO;

    @Autowired
    private ChapterDAO chapterDAO;


    public String getArticleText(Long id) {
        ArticleDO articleDO = articleDAO.selectById(id);
        if (articleDO == null) {
            throw new WlxkException("文章不存在");
        }
        List<ChapterDO> chapterDOS = chapterDAO.selectList(new LambdaQueryWrapper<ChapterDO>()
                .eq(ChapterDO::getArticleId, articleDO.getId().toString()).orderByAsc(ChapterDO::getChapterId));
        if (CollectionUtils.isEmpty(chapterDOS)) {
            throw new WlxkException("文章不存在");
        }
        StringBuilder sBuilder = new StringBuilder();
        chapterDOS.forEach(item-> sBuilder.append("    ").append(item.getName()).append("\n").append(item.getContent()).append("\n"));
        return sBuilder.toString();
    }

}
